package com.lc.rongzecaifu.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.rongzecaifu.MyApplication;
import com.lc.rongzecaifu.conn.JsonVip;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.zcx.helper.bound.BoundViewHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 4/13/2016.
 */
public class ApplyVipAdapter extends RecyclerView.Adapter<ApplyVipAdapter.ViewHolder> {

    private int select = 0;
    private Context context;
    private List<JsonVip.Info.ListContent> list;

    private Map<Integer, Boolean> checkMap = new HashMap<>();

    MyItemClickListener listener = null;

    public ApplyVipAdapter(Context context, List<JsonVip.Info.ListContent> list) {
        this.context = context;
        this.list = list;

//        if (list.size() > 0) {
//
//            for (int i = 0; i < list.size(); i++) {
//
//                checkMap.put(i, false);
//
//            }
//        }
    }

    public void notifyDataSetChanged(List<JsonVip.Info.ListContent> list) {

        for (int i = 0; i < list.size(); i++) {

            checkMap.put(i, false);

        }

        notifyDataSetChanged();

    }

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        // each data item is just a string in this case
        @Bind(R.id.iv_01)
        SimpleDraweeView iv01;
        @Bind(R.id.iv_02)
        ImageView iv02;
        @Bind(R.id.tv_01)
        TextView tv01;
        private MyItemClickListener mListener;

        public ViewHolder(View view, MyItemClickListener listener) {
            super(view);
            ButterKnife.bind(this, view);
            this.mListener = listener;
            view.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClick(getPosition());
            }
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.gird_01_item_view, null)));

        ViewHolder vh = new ViewHolder(view, listener);

        return vh;
    }

    /**
     * 设置Item点击监听
     *
     * @param listener
     */
    public void setOnItemClickListener(MyItemClickListener listener) {
        this.listener = listener;
    }

    public interface MyItemClickListener {
        void onItemClick(int postion);
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        holder.tv01.setText(list.get(position).real_name);

        holder.iv01.setImageURI(Uri.parse(list.get(position).avatar));

        holder.iv02.setSelected(checkMap.get(position));

    }

    public void selectOnly(final int position) {

        for (int i = 0; i < checkMap.size(); i++) {

            if (i == position) {

                checkMap.put(position, true);

            } else {

                checkMap.put(i, false);

            }

        }

    }

    /**
     * Returns the total number of items in the data set hold by the adapter.
     *
     * @return The total number of items in this adapter.
     */
    @Override
    public int getItemCount() {
        return list.size();
    }

}
